<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/11/20
 * Time: 11:10
 * BannerController.php
 */

namespace App\Http\Controllers\Api\Shop;


use App\Http\Controllers\Api\BaseAdminController;
use App\Http\Requests\Banner\AddBannerRequest;
use App\Http\Requests\Banner\BannerDetailRequest;
use App\Http\Requests\Banner\DeleteBannerRequest;
use App\Http\Requests\Banner\EditBannerRequest;
use App\Http\Requests\Banner\GetBannerPageRequest;
use App\Http\Requests\Banner\UpdateStatusRequest;
use App\Http\Transformers\Banner\BannerDetailTransformer;
use App\Http\Transformers\Banner\GetBannerPageTransformer;
use App\Repositories\BannerRepository;
use App\Traits\FilterParameters;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

/**
 * Class BannerController
 * @package App\Http\Controllers\Api\Shop
 */
class BannerController extends BaseAdminController
{
    use FilterParameters;

    /**
     * @var BannerRepository 注入的数据层
     */
    protected $bannerRepository;

    /**
     * BannerController constructor.
     * @param BannerRepository $bannerRepository
     */
    public function __construct(BannerRepository $bannerRepository)
    {
        $this->bannerRepository = $bannerRepository;
    }

    /**
     * 获取banner分页列表
     * @param GetBannerPageRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getBannerPage(GetBannerPageRequest $request){
        $limit = $request->get('limit');
        $data = $request->except(['limit', 'page']);

        $banner = $this->bannerRepository->getBannerPageRepository($data, $limit);

        return $this->response->paginator($banner, new GetBannerPageTransformer());
    }

    /**
     * 新增banner
     * @param AddBannerRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function addBanner(AddBannerRequest $request){
        $all = $request->all();

        $data = $this->handleFilterBannerCondition($all);
        if($this->bannerRepository->addBannerRepository($data)){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('保存banner失败！');
    }

    /**
     * 编辑banner
     * @param EditBannerRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function editBanner(EditBannerRequest $request){
        $all = $request->all();
        $id = $request->get('id');

        $data = [];
        $data['data'] = $this->handleFilterBannerCondition($all);
        $data['imageAttachment'] = data_get($all, 'imageAttachment');
        if($this->bannerRepository->editBannerRepository($data, $id)){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('编辑banner失败！');
    }

    /**
     * 更新banner状态
     * @param UpdateStatusRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateStatus(UpdateStatusRequest $request){
        $all = $request->all();
        if($this->bannerRepository->updateStatusRepository($all)){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('修改banner状态失败！');

    }

    /**
     * 删除banner
     * @param DeleteBannerRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deleteBanner(DeleteBannerRequest $request){
        $id = $request->get('id');

        if($this->bannerRepository->deleteBannerRepository($id)){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('删除banner失败！');
    }

    /**
     * 获取banner详情
     * @param BannerDetailRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function bannerDetail(BannerDetailRequest $request){

        $id = $request->get('id');

        $data = $this->bannerRepository->getBannerDetailRepository($id);

        return $this->response->item($data, new BannerDetailTransformer());
    }

}
